package com.infore.qualityControl.export.policy.handler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.infore.qualityControl.export.policy.bo.ExportParamBo;
import com.infore.qualityControl.export.policy.strategy.Strategy;

/**
 * 导出表格策略模式管理器
 * 
 * @author Cold
 *
 */
@Component
public class ExportHandler {

	/**
	 * 使用线程安全的ConcurrentHashMap存储所有实现Strategy接口的Bean 
	 * key:beanName
	 * value：实现Strategy接口Bean
	 */
	private final Map<String, Strategy> strategyMap = new ConcurrentHashMap<>();

	// 注入所有实现了Strategy接口的Bean
	@Autowired
	public ExportHandler(Map<String, Strategy> strategyMap) {
		this.strategyMap.clear();
		strategyMap.forEach((k, v) -> this.strategyMap.put(k, v));
	}

	public byte[] getResource(String code, ExportParamBo exportParamBo) throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		byte[] bytes = {};
		if ("1".equals(exportParamBo.getFlag())) { // excel
		    bytes = strategyMap.get(code).doExcelStrategy(list, exportParamBo);
		} else if ("2".equals(exportParamBo.getFlag())) { // pdf
		    bytes = strategyMap.get(code).doPdfStrategy(list, exportParamBo);
		} else if ("3".equals(exportParamBo.getFlag())) { // word
		    bytes = strategyMap.get(code).doWordStrategy(list, exportParamBo);
		}
		return bytes;
	}

}
